package cn.topcodes.tcsf.app.service.impl;

import cn.topcodes.tcsf.app.domain.entity.AppUserParam;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

/**
 * 用户参数 服务
 * @author unicorn
 * @date 2017-11-13 22:06:58
 */
@Service
public class AppUserParamServiceImpl extends GenericService<AppUserParam> {

    @Autowired
    private AppUserServiceImpl appUserService;

    public AppUserParam findByPropName(Long appUserId, String name) {
        Assert.hasText(name, "参数名称不能为空");
        Assert.isTrue(this.appUserService.exist(appUserId), "用户不存在");
        Example example = this.buildExample();
        example.or()
                .andEqualTo(AppUserParam.FIELD_PROP_NAME, name)
                .andEqualTo(AppUserParam.FIELD_APP_USER_ID, appUserId);
        AppUserParam param = this.findOne(example);
        if(param == null) {
            param = new AppUserParam();
            param.setAppUserId(appUserId);
            param.setPropName(name);
        }
        return param;
    }
}
